<?php

namespace app\admin\service;

use app\admin\dao\LawCaseDao;
use app\admin\model\UM as UM;
use app\common\model\LawCase;
use ShidianUtil\Exception\DataNotExistException;
use ShidianUtil\Utils\TimeUtil;
use think\exception\ValidateException;

/**
 * Class LawCaseService
 * @package app\law_case\service
 */
class LawCaseService
{
    private LawCaseDao $lawCaseDao;

    public function __construct(LawCaseDao $lawCaseDao)
    {
        $this->lawCaseDao = $lawCaseDao;
    }

    /**
     * @param UM|null $user
     * @param $action
     * @param $categoryId
     * @param $provinceId
     * @param $cityId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function index(?UM $user, $isPublish, $categoryId, $provinceId, $cityId, $startTime, $endTime, $kw, ?int $page = null, ?int $page_size = null): array
    {
        if(is_numeric($endTime)){
            $endTime = TimeUtil::stamp(TimeUtil::DAY, $endTime, false);
        }
        return $this->lawCaseDao->listLawCases($isPublish, $categoryId, $provinceId, $cityId, $startTime, $endTime, $kw, $page, $page_size);
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return LawCase
     */
    public function read(?UM $user, int $id): ?LawCase
    {
        //这里如果要修改查询，建议把查询放到dao层去
        return $this->lawCaseDao->findByPK($id, true);
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  LawCase
     */
    public function add(UM $user, array $params): LawCase
    {
        return $this->lawCaseDao->insertActive($params, []);
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     * @return LawCase
     */
    public function modify(UM $user, int $id, array $params): LawCase
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = LawCase::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该对象不存在或已被删除');
        }

        $allowField = [];
        $this->lawCaseDao->updateActive($record, $params, $allowField);

        return $record;
    }

    /**
     * 删除
     * @param UM $user
     * @param int $id
     */
    public function rm(UM $user, int $id): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = LawCase::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            return;
        }

        $this->lawCaseDao->deleteModel($record);
    }

    /**
     * 发布案源
     * @param UM $user
     * @param int $id
     * @param array $params
     * @return LawCase
     */
    public function modifyPublish(UM $user, int $id, array $params): LawCase
    {
        params_enums($params, 'isPublish', 1, 2);
        $params['publishTime'] ??= time();
        $params['totalPrice'] ??= null;
        $params['num'] ??= 3;
        $params['surplus'] = $params['num'];

        if ($params['isPublish'] == 1) {
            if (!is_numeric($params['totalPrice'])) {
                throw new ValidateException('请输入正确的总价');
            }
            $params['totalPrice'] = round($params['totalPrice'], 2);
            if ($params['totalPrice'] <= 0) {
                throw new ValidateException('总价应该大于0');
            }

            if (!is_numeric($params['num'])) {
                throw new ValidateException('请输入正确的数量');
            }
            $params['num'] = round($params['num'], 0);
            if ($params['num'] < 1) {
                throw new ValidateException('数量应该大于等于1');
            }

            if (bcmod($params['totalPrice'] * 100, $params['num']) != 0) {
                throw new ValidateException('总价应该是数量的整数倍');
            }
            $params['price'] = $params['totalPrice'] / $params['num'];

        }

        //这里如果要修改查询，建议把查询就放到这里
        $record = LawCase::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该案源不存在或已被删除');
        }

        // 提交人提成20%
        $creatorShareRate = 0.2;

        if ($params['isPublish'] == 1) {

            $record->isPublish = $params['isPublish'];
            $record->publishTime = $params['publishTime'];
            $record->totalPrice = $params['totalPrice'];
            $record->price = $params['price'];
            $record->num = $params['num'];
            $record->surplus = $params['surplus'];
            $record->creatorShareRate = $creatorShareRate;
            $record->save();

            // 发布案源后，所有律师更新红点？数量太多，这个红点不更新！

        } else {

            $record->isPublish = $params['isPublish'];
            $record->save();
        }

        return $record;
    }

}
